package com.reminiscence.main;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.reminiscence.classes.Utente;
import com.reminiscence.database.OperazioniDB;

public class ListaStorie2 extends Activity
{
	public static ArrayList<HashMap<String, String>> listaStorie = new ArrayList<HashMap<String, String>>();
	public static ListView lw;
	public Utente ut = new Utente();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_storie);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		Bundle extras = getIntent().getExtras();
		String flag;
		
		
		ut.leggiUtenteA(this);
		
				
		TextView txtListaStorieIntestazione1 = (TextView)findViewById(R.id.txtListaStorieIntestazione1);
		txtListaStorieIntestazione1.setText("Lista delle storie di " + ut.nome + "\n");
		
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("IdUtente", ut.nomeUtente));
		params.add(new BasicNameValuePair("AC", "a"));
		
		Boolean ret = false; 
		try
		{
			ret =  (Boolean)(new OperazioniDB(this, "listaStorie", params).execute().get());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		lw = (ListView)findViewById(R.id.lstStorie);
		lw.setAdapter(null);
		
		ListAdapter adapter = new SimpleAdapter
		(
			this, listaStorie,
			R.layout.list_item_storie,
			new String[]
			{
				"idStoria",
				"titolo",
				"anno",
				"dataPub"
			},
			new int[]
			{
				R.id.txtListaStorieIdStoria,
				R.id.txtListaStorieTitolo,
        		R.id.txtListaStorieAnno,
        		R.id.txtListaStorieDataPub
    		}
		);

		lw.setAdapter(adapter);
		lw.setClickable(true);
		
		lw.setOnItemClickListener
		(
			new AdapterView.OnItemClickListener()
			{
				
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
				{
					View curr;
					TextView tw;
					
					int k = ListaStorie.lw.getCount();
					
					int idArray[] = new int[k];
					for (int i = 0; i< k; i++)
					{
						curr = arg0.getChildAt(i);
						tw = (TextView)curr.findViewById(R.id.txtListaStorieIdStoria);
						idArray[i] = Integer.parseInt(tw.getText().toString());
					}
					
					curr = arg0.getChildAt((int) arg3);
					tw = (TextView)curr.findViewById(R.id.txtListaStorieIdStoria);
					String s = tw.getText().toString();
			
					Intent i = new Intent(ListaStorie2.this, Timeline.class);
					i.putExtra("IdStoria", s);
					i.putExtra("idArray", idArray);
					i.putExtra("maxStorie", k);
					i.putExtra("idCurr", (int)arg3);
					startActivity(i);
				}
			}
		);
	}
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.lista_storie, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(item.getItemId() == R.id.mnuProfilo){
			
			Intent j = new Intent(this, ProfiloCompleto.class);
			j.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(j);
			
		}
		return false;
	}
}
